/**
 * @Author: oldTea
 * @CreateTime: 2024-11-25
 * @Description:
 * @Version: 1.0
 */
package com.ambition.demo.juc;

/**
 * Object类的线程唤醒与阻塞  wait  notify  需配合synchronized使用
 *@Author: oldTea
 *@CreateTime: 2024-11-25
 *@Description:
 *@Version: 1.0
 */
public class ObjectWait {

    public static void main(String[] args) {
        Object o = new Object();
        Thread thread = new Thread(() -> {
            synchronized (o){
                try {
                    System.out.println("线程A被阻塞之前");
                    o.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("线程A被线程B唤醒");
        },"A线程");
        thread.start();

        new Thread(() -> {
            synchronized (o){
                o.notify();
                System.out.println("线程B唤醒线程A");
            }
        },"线程B").start();;
    }
}

